/*
 * BaseAction.java
 * 
 * Version information
 * 
 * April 10, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */

package net.amsoft.iservice.ilink.webapp.struts.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.amsoft.iservice.ilink.service.ILinkService;
import net.amsoft.iservice.ilink.util.ILinkConstants;
import net.amsoft.iservice.ilink.webapp.dataObject.IBrokerData;
import net.amsoft.iservice.ilink.webapp.dataObject.LoginData;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * An action class that extends struts action class and defines default actions
 * for the application.
 * 
 * @author mohit
 * 
 */

public abstract class BaseAction extends Action {
	public static final Logger oLogger = Logger.getLogger(BaseAction.class);

	public final ActionForward execute(ActionMapping oMapping,
			ActionForm oForm, HttpServletRequest oRequest,
			HttpServletResponse oResponse) {

		oLogger.info("execute() : entry");
		preProcess(oMapping, oForm, oRequest, oResponse);
		ActionForward oForward = null;
		ActionMessages oMsgs = new ActionErrors();
		try {
			oForward = process(oMapping, oForm, oRequest, oResponse);
		} catch (Exception e) {
			oLogger.warn("execute() : Exception : " + e.getMessage());
			if (e instanceof IServiceException) {
				IServiceException oISE = (IServiceException) e;
				oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(oISE
						.getMessage(), false));
				saveErrors(oRequest, oMsgs);
				oLogger.warn("execute() : Exception errorcode: "
						+ oISE.getErrorCode() + ","
						+ IServiceConstants.getError(oISE.getErrorCode()));
			}
			oLogger.info("execute() : exit");
			return oMapping.findForward(ILinkConstants.FWD_CHKLOGIN);
		}
		postProcess(oMapping, oForm, oRequest, oResponse);
		oLogger.info("execute() : exit, return Forward: " + oForward);
		return oForward;
	}

	public void preProcess(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse) {
		oLogger.info("preProcess() : entry");

		// get the existing session
		HttpSession ohSession = oRequest.getSession(false);
		LoginData oLoginData = (LoginData) ohSession
				.getAttribute(ILinkConstants.SESSION_LOGIN_OBJ);
		if (oLoginData == null) {
			oLoginData = new LoginData();
			ohSession
					.setAttribute(ILinkConstants.SESSION_LOGIN_OBJ, oLoginData);
			oLogger.info("preProcess() : new LoginData created");
		}
		Object oIBrokerData = ohSession
				.getAttribute(ILinkConstants.SESSION_IBROKER_OBJ);
		if (oIBrokerData == null) {
			oIBrokerData = new IBrokerData();
			loadIBrokerData((IBrokerData) oIBrokerData, oRequest
					.getServerName());
			ohSession.setAttribute(ILinkConstants.SESSION_IBROKER_OBJ,
					oIBrokerData);
			oLogger.info("preProcess() : new IBrokerData created");
		}
	}// preProcess() ends

	public abstract ActionForward process(ActionMapping oMapping,
			ActionForm oForm, HttpServletRequest oRequest,
			HttpServletResponse oResponse) throws Exception;

	public void postProcess(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse) {
	}

	private Map loadIBrokerData(IBrokerData oIBData, String ibrokerName) {
		oLogger.info("loadIBrokerData() : entry");
		Map oIBMap = null;
		try {
			oIBMap = ILinkService.getIBrokerData(ibrokerName);
			if (oIBMap.keySet().contains(IServiceConstants.IB_BANNER_KEY)) {
				oIBData.setBannerLink((String) oIBMap
						.get(IServiceConstants.IB_BANNER_KEY));
				oIBMap.remove(IServiceConstants.IB_BANNER_KEY);
			}
		} catch (Exception e) {
			oLogger.warn("loadIBrokerData(): Exception : " + e.getMessage());
		}
		oIBData.setIBrokerMap(oIBMap);
		oLogger.info("loadIBrokerData() : Returning IBMap=" + oIBMap);
		return oIBMap;
	}
}
